package com.yxe.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.yxe.api.CommonResult;
import com.yxe.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author X
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation("获取验证码【三分钟内有效】:用于登录")
    @GetMapping("/getAuthCodeToLogin")
    public CommonResult getAuthCodeToLogin(@ApiParam("用户手机号") @RequestParam String telephone) {
        Boolean flag = userService.generateLoginAuthCode(telephone);
        if (flag) {
            return CommonResult.success(null, "发送成功");
        } else {
            return CommonResult.failed("发送失败");
        }
    }

    @ApiOperation("用户使用手机号加验证码进行登录【新用户将直接注册】")
    @PostMapping(value = "/loginByAuthCode")
    public CommonResult loginByAuthCode(@ApiParam("用户手机号") @RequestParam String telephone,
                                        @ApiParam("验证码") @RequestParam String authCode) {
        SaTokenInfo saTokenInfo = userService.loginByAuthCode(telephone, authCode);
        if (saTokenInfo == null) {
            return CommonResult.validateFailed("验证码错误或已失效，登录失败");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", saTokenInfo.getTokenValue());
        tokenMap.put("tokenHead", saTokenInfo.getTokenName());
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("用户使用手机号加密码进行登录")
    @PostMapping(value = "/loginByPwd")
    public CommonResult loginByPwd(@ApiParam("用户手机号") @RequestParam String telephone,
                                   @ApiParam("登录密码") @RequestParam String password) {
        SaTokenInfo saTokenInfo = userService.loginByPwd(telephone, password);
        if (saTokenInfo == null) {
            return CommonResult.validateFailed("手机号或密码错误，登录失败！");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", saTokenInfo.getTokenValue());
        tokenMap.put("tokenHead", saTokenInfo.getTokenName());
        return CommonResult.success(tokenMap);
    }

    @ApiOperation(value = "修改登录密码")
    @PostMapping(value = "/updatePassword")
    public CommonResult updatePassword(@ApiParam("验证码") @RequestParam String authCode,
                                       @ApiParam("新密码") @RequestParam String newPassword) {

        Boolean flag = userService.updatePasswordById(authCode, newPassword);
        if (flag) {
            return CommonResult.success("密码修改成功！");
        } else {
            return CommonResult.failed("密码修改失败！");
        }
    }

    @ApiOperation("修改头像")
    @PostMapping(value = "/uploadHeadPortrait")
    public CommonResult uploadHeadPortrait(@ApiParam("头像图片") @RequestParam("file") MultipartFile multipartFile) {
        userService.uploadHeadPortrait(multipartFile);
        return CommonResult.success("头像上传成功");
    }

    @ApiOperation("获取修改验证码【三分钟内有效】:用于修改密码，不用输入手机号")
    @GetMapping("/getAuthCodeToUpdate")
    public CommonResult getAuthCodeToUpdate() {
        Boolean flag = userService.generateUpdateAuthCode();
        if (flag) {
            return CommonResult.success(null, "发送成功");
        } else {
            return CommonResult.success(null, "发送失败");
        }
    }

    @ApiOperation("修改昵称")
    @GetMapping("/updateNickname")
    public CommonResult updateNickname(@ApiParam("昵称") @RequestParam String nickname) {
        Integer affectRows = userService.updateNickName(nickname);
        if (affectRows > 0) {
            return CommonResult.success(null, "修改成功");
        } else {
            return CommonResult.success(null, "修改失败");
        }
    }


    /*@ApiOperation("用户使用手机号加密码进行登录")
        @PostMapping(value = "/loginByPwd")
        public CommonResult loginByPwd(@ApiParam("用户手机号") @RequestParam String telephone,
                                       @ApiParam("登录密码") @RequestParam String password) {
            String token = userService.loginByPwd(telephone, password);
            if (token == null) {
                return CommonResult.validateFailed("手机号或密码错误，登录失败！");
            }
            Map<String, String> tokenMap = new HashMap<>();
            tokenMap.put("token", token);
            tokenMap.put("tokenHead", tokenHead);
            return CommonResult.success(tokenMap);
        }*/

    /*@ApiOperation("是否登录")
    @PostMapping(value = "/isLogin")
    public CommonResult isLogin() {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        System.out.println("tokenInfo = " + tokenInfo);
        boolean login = StpUtil.isLogin();
        System.out.println("login = " + login);
        return CommonResult.success("111");
    }*/

    /*@ApiOperation(value = "获取当前登录用户信息【用户id和用户手机号】")
    @GetMapping(value = "/info")
    public CommonResult getAdminInfo(HttpServletRequest request) {
        String fullToken = request.getHeader(tokenHeader);
        Map<String, String> adminInfo = userService.getAdminInfo(fullToken);
        return CommonResult.success(adminInfo);
    }*/

}
